<template>
  <div>
    <el-card class="box-card" style="margin: -30px 80px 0 80px;">
      <span style="font-size: 18px; font-family: 行楷; font-weight: bold;">发布公告</span>
      <div class="form" style="margin-top: 15px;">
        <!-- {{ form }} -->
        <el-form ::model="form">
          <el-form-item label="公告标题" :label-width="formLabelWidth"> 
            <el-input v-model="form.title"></el-input>
          </el-form-item>
          <el-form-item label="请选择分类">
            <el-select clearable v-model="form.categoryId" placeholder="请选择栏目分类">
              <el-option v-for="item in categories" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="作者" :label-width="formLabelWidth">
            <el-input v-model="realname">123</el-input>
          </el-form-item>
          <el-form-item label="正文" :label-width="formLabelWidth">
            <Tinymce v-model="form.content" :height="300"></Tinymce>
          </el-form-item>
          <!-- 返回按钮和发布按钮 -->
          <div class="button" style="text-align: right;">
            <el-button type="primary" style="margin-left: 20px;" @click="back">返回</el-button>
            <el-button type="primary" style="margin-left: 20px;" @click='toAdd'>发布</el-button>
          </div>
        </el-form>
      </div>
    </el-card>
  </div>
</template>
<script>
import { get, post } from '@/utils/request'
import { mapGetters } from 'vuex'

// 引入富文本编辑器 
import Tinymce from '@/components/Tinymce'
export default {
  computed: {
    ...mapGetters([
      'realname'
    ])
  },
  components: {
    Tinymce
  },
  data() {
    return {
      articleDetails: {},
      formLabelWidth: '80px',
      categories: {},
      form: {}
    }
  },
  methods: {
    // 获取栏目
    async getCategories() {
      let res = await get('/category/findAll')
      // console.log(res);
      this.categories = res.data
    },
    // 返回上一级
    back() {
      this.$router.go(-1)
    },
    // 新增公告
    async toAdd() {
      this.form.publishTime = Date.now()
      this.form.author = this.realname
      console.log(this.form);
      let res = await post('/article/addArticle',this.form)
      // console.log(res);
      if (res.status === 200) {
        this.$notify({
          title: '成功',
          message: res.message,
          type: 'success'
        })
        // 发布成功跳转到资讯列表页面
        this.$router.push({
          path: '/manage/notice'
        })
      } else {
        this.$notify.error({
          title: '失败',
          message: res.message,
          type: 'info'
        })
      }
    }
  },
  created() {
    this.articleDetails = this.$route.query;
    this.getCategories()
  },
}
</script>